﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Keepo.Models;
using System.IO;
using AlbumDesigner.Helpers;

namespace AlbumDesigner.Models.Types
{
    public class AlbumPrintVersionData
    {
        public int AlbumPrintVersionId { get; set; }

        public int VersionIdx { get; set; }

        public int AlbumId { get; set; }
        public string Album { get; set; }

        public int AlbumPrintVersionStatusId { get; set; }
        public string AlbumPrintVersionStatus { get; set; }

        public string VersionDate { get; set; }

        public string PdfSentDate { get; set; }

        public string PrintSentDate { get; set; }

        public bool ShowToAlbumEditor { get; set; }

        public string InnerPdfFilePath { get; set; }
        public string CoverPdfFilePath { get; set; }
        public string InnerPdfSize { get; set; }
        public string CoverPdfSize { get; set; }
        public string PdfFilesSize { get; set; }

        public AlbumPrintVersionData(AlbumPrintVersion printVersion)
        {
            AlbumPrintVersionId = printVersion.AlbumPrintVersionId;
            VersionIdx = printVersion.VersionIdx;
            AlbumId = printVersion.AlbumId;
            Album = printVersion.Album.Title;
            AlbumPrintVersionStatusId = printVersion.AlbumPrintVersionStatusId;
            AlbumPrintVersionStatus = printVersion.AlbumPrintVersionStatus.Title;
            VersionDate = printVersion.VersionDate.ToString("dd/MM/yyyy HH:mm");
            ShowToAlbumEditor = printVersion.ShowToAlbumEditor;

            if (printVersion.PdfSentDate != null)
            {
                PdfSentDate = printVersion.PdfSentDate.Value.ToShortDateString();
            }

            if (printVersion.PrintSentDate != null)
            {
                PrintSentDate = printVersion.PrintSentDate.Value.ToShortDateString();
            }

            var innerPdfPath = HttpContext.Current.Server.MapPath("~/" + printVersion.InnerUrl);
            var coverPdfPath = HttpContext.Current.Server.MapPath("~/" + printVersion.CoverUrl);

            long innerPdfSize = 0;
            long coverPdfSize = 0;
            InnerPdfFilePath = "";
            CoverPdfFilePath = "";

            //search the INNER pdf file of this version
            if (File.Exists(innerPdfPath))
            {
                InnerPdfFilePath = printVersion.InnerUrl;

                innerPdfSize = new FileInfo(innerPdfPath).Length;
                InnerPdfSize = FilesHelper.BytesToMegaBytes(innerPdfSize);
            }

            //search the COVER pdf file of this version
            if (File.Exists(coverPdfPath))
            {
                CoverPdfFilePath = printVersion.CoverUrl;

                coverPdfSize = new FileInfo(coverPdfPath).Length;
                CoverPdfSize = FilesHelper.BytesToMegaBytes(coverPdfSize);
            }

            PdfFilesSize = FilesHelper.BytesToMegaBytes(innerPdfSize + coverPdfSize);
        }
    }
}